﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace HistogramPackage
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnRunHistogram_Click(object sender, EventArgs e)
        {
            if (timer1.Enabled)
            {
                timer1.Enabled = false;
                btnRunHistogram.Text = "Run";
            }
            else
            {
                timer1_Tick(sender, e);
                timer1.Enabled = true;
                btnRunHistogram.Text = "Stop";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            btnRunHistogram.Text = "Run";
            timer1_Tick(sender, e);
        }

        private static readonly string sourcesFile = "sources.csv";
        Histogram hist;

        private void timer1_Tick(object sender, EventArgs e)
        {
            // load filenames
            CSVKit.CSVTable csvFile;
            IEnumerator<List<String>> rowIt;
            List<string> files = new List<string>();

            csvFile = new CSVKit.CSVTable(sourcesFile);
            rowIt = csvFile.getRowIterator();
            while (rowIt.MoveNext())
            {
                try
                {
                    files.Add(rowIt.Current[0]);
                }
                catch
                {
                    // ignore
                }
            }

            string datafile = files[new Random().Next(0, files.Count)];
            List<Tuple<double, int>> samples = new List<Tuple<double, int>>();
            csvFile = new CSVKit.CSVTable(datafile);

            rowIt = csvFile.getRowIterator();
            while (rowIt.MoveNext())
            {
                try
                {
                    samples.Add(new Bucket(rowIt.Current));
                }
                catch
                {
                    // ignore
                }
            }

            int bucketCount = new Random().Next(16, 96);
            hist = new Histogram(samples);
            hist.BarCount = bucketCount;

            PDF pdf = new GeneralizedPareto();
            pdf.extractGreekValues();
            hist.AddPDF(pdf, Color.ForestGreen, "Generalized Pareto");
            

            pdf = new InverseBurr();
            pdf.extractGreekValues();
            hist.AddPDF(pdf, Color.Firebrick, "Inverse Burr");

            lblCurrentFile.Text = datafile;
            lblBuckets.Text = bucketCount.ToString() + " buckets";

            redraw();
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (hist == null)
                timer1_Tick(sender, e);
            else
                redraw();
        }

        private void redraw()
        {
            hist.ImageSize = pnlTest.Size;
            pnlTest.BackgroundImage = hist.GetHistogram();

            PDF pdf = new InverseBurr();
            pdf.extractGreekValues();
            pnlTest2.BackgroundImage = hist.GetHistogramBitmapWithColor(pnlTest2.Size, Color.BurlyWood, pdf, false, 1);

            float lineWeight = hist.LineWeight;

            hist.ImageSize = pnlTest3.Size;
            hist.LineWeight = 1;
            pnlTest3.BackgroundImage = hist.GetHistogram();

            hist.ImageSize = pnlTest4.Size;
            hist.LineWeight = 1;
            pnlTest4.BackgroundImage = hist.GetHistogram();

            hist.LineWeight = lineWeight;

            this.Invalidate();
            this.Update();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1_Tick(sender, e);
        }
    }
}
